package com.ayoka.ui.course.detail;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;

import com.ayoka.R;
import com.ayoka.mvp.CourseQuestionMVP.CourseQuestionModel.CourseQuestionBean;
import com.ayoka.mvp.CourseQuestionMVP.CourseQuestionPresenter;
import com.ayoka.mvp.CourseQuestionMVP.ICourseQuestionView;
import com.jcp.baselibrary.dialog.BaseDialogBuilder;
import com.jcp.baselibrary.dialog.BaseDialogFragment;
import com.jcp.baselibrary.utils.TDevice;

import java.util.List;

/**
 * Created by jinchunping on 2019/1/23.
 */

public class QuestionInputDialog extends BaseDialogFragment implements ICourseQuestionView {

    public static QuestionInputDialog newInstance(Builder builder) {
        QuestionInputDialog dialog = new QuestionInputDialog();
        Bundle bundle = getArgumentBundle(builder);
        bundle.putLong("CourseId", builder.courseId);
        dialog.setArguments(bundle);
        return dialog;
    }

    private CourseQuestionPresenter questionPresenter;

    private long courseId;

    private EditText edtQuestion;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        courseId = getArguments().getLong("CourseId");
        questionPresenter = new CourseQuestionPresenter();
        questionPresenter.attach(this);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        Window window = dialog.getWindow();
        //设置软键盘通常是可见的
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        return dialog;
    }

    @Override
    protected void convertView(@Nullable View contentView, BaseDialogFragment dialogFragment) {
        edtQuestion = contentView.findViewById(R.id.edt_input);
        edtQuestion.requestFocus();
        TDevice.showSoftKeyboard(edtQuestion);
        final ImageButton iBtnSend = contentView.findViewById(R.id.ibtn_send);
        iBtnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (edtQuestion.length() == 0) {
                    return;
                }
                String question = edtQuestion.getText().toString().trim();
                questionPresenter.submitQuestion(courseId, question);
                iBtnSend.setEnabled(false);
                TDevice.hideSoftKeyboard(edtQuestion);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        edtQuestion.requestFocus();
        TDevice.showSoftKeyboard(edtQuestion);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_course_question_input;
    }

    @Override
    public void showWaitDialog(String message) {

    }

    @Override
    public void hideWaitDialog() {

    }

    @Override
    public void showToast(String message) {

    }

    @Override
    public Snackbar showSnackBar(View view, String content, int type) {
        return null;
    }

    @Override
    public void onDestroy() {
        if (questionPresenter != null) {
            questionPresenter.detachView();
            questionPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void configCourseQuestionListData(List<CourseQuestionBean> courseQuestionBeanList) {

    }

    @Override
    public void submitQuestionCallback(boolean isSuccess) {
        dismissAllowingStateLoss();
        if (mDialogResultListener != null){
            mDialogResultListener.result(this, isSuccess);
        }
    }

    public static class Builder extends BaseDialogBuilder<Builder, QuestionInputDialog> {

        private long courseId;

        public Builder setCourseId(long courseId) {
            this.courseId = courseId;
            return this;
        }

        @Override
        public QuestionInputDialog build() {
            return QuestionInputDialog.newInstance(this);
        }
    }
}